package com.tools;

import com.entity.User;
import com.service.UserService;
import org.aspectj.apache.bcel.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.log4j.Logger;

import javax.annotation.Resource;
import javax.management.relation.Role;
import javax.management.relation.RoleStatus;

import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    private Logger logger = Logger.getLogger("UserController.class");
    private ArrayList<User> userList =new ArrayList<User>();
    @Resource
    private UserService userService;
    private ArrayList<User> queryUserList =new ArrayList<User>();
    @Resource
    private RoleStatus roleStatus;
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public String list(@RequestParam(value = "userName",required = false)String userName, Model model){
        logger.info("查询：userName:"+userName+",userList====");
        if(userName != null&&! userName.equals("")){
            for(User user:userList) {
                if (user.getUserName().indexOf(userName) != -1) {
                    queryUserList.add(user);
                }
            }
            model.addAttribute("queryUserList",queryUserList);
        }else{
            model.addAttribute("queryUserList",userList);
        }
        return "user/userlist";
    }
    @RequestMapping(value = "/login.html")
    public String login(){
        logger.debug("UserController welcome SMBMS============");
     return "login";
    }

    public UserController(){
        try {   //初始化用户数据
            userList.add(new User(1,"test001","测试用户001","1111111",1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1986-12-10"),
                    "13566669998","北京市朝阳区北苑",1,1,new Date(),1,new Date()));
            userList.add(new User(2,"zhaoyan","赵燕","2222222",1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1984-11-10"),
                    "18635375424","北京市海淀区成府路",1,1,new Date(),1,new Date()));
            userList.add(new User(3,"test001","测试用户003","3333333",1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1980-11-11"),
                    "13134565428","北京市通州北苑",1,1,new Date(),1,new Date()));
            userList.add(new User(4,"wanglin","王林","4444444",1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1989-09-10"),
                    "18945347643","北京市学院路",1,1,new Date(),1,new Date()));
            userList.add(new User(5,"zhaojing","赵静","5555555",1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1981-08-01"),
                    "13034566343","北京市广安门",1,1,new Date(),1,new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/dologin.html",method = RequestMethod.POST)
    public String doLogin(@RequestParam String userCode, @RequestParam String userPassword){
        logger.debug("doLong------");

        com.entity.User user = userService.login(userCode,userPassword);

        if(null!=user){
            return "redirect:/user/main.html";
        }else{
            return "login";
        }
    }

    @RequestMapping(value="exlogin.html",method=RequestMethod.GET)
    public String exLogin(@RequestParam String usercode,@RequestParam String userPassword){
        logger.debug("exLogin===============");
        User user = userService.login(usercode,userPassword);
        if(null ==user){
            throw new RuntimeException("用户名或者密码不正确");
        }
        return "redirect:/user/main.html";
    }
    /*@ExceptionHandler(value ={RuntimeException.class})
    public String handlerException(RuntimeException e,HttpServletRequest req){
        req.setAttribute("e",e);
        return "error";
    }*/

   // @RequestMapping(value = "/main.html");
    public String main(){
        return "frame";
    }
    public String getUserList(Model model,
        @RequestParam(value = "queryname",required = false)String queryUserName,
        @RequestParam(value = "queryUserRole",required = false)String queryUserRole,@RequestParam(value = "pageIndex",required = false)String pageIndex){
        logger.info("getUserList--->queryUserName:"+queryUserName);
        logger.info("getUserList--->queryUserRole:"+queryUserRole);
        logger.info("getUserLsit===>pageIndex:"+pageIndex);
        int _queryUserRole=0;
        List<User> userList = null;
        //int pageSize =Constants.pageSize;
        int currentPageNo=1;
        if(queryUserName==null){
            queryUserName="";
        }
        if(queryUserRole!=null&&!queryUserRole.equals("")){
            _queryUserRole =Integer.parseInt(queryUserName);
        }
        if(pageIndex!=null){
            try{
                currentPageNo = Integer.valueOf(pageIndex);
            }catch (NumberFormatException e){
                return "redirect:user/syserror.hrml";
            }
        }
        int totalCount =userService.getUserCount(queryUserName,_queryUserRole);
        PageSupport pages= new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount =pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalCount){
            currentPageNo = totalPageCount;
        }
        userList = userService.getUserList(queryUserName,_queryUserRole,currentPageNo,pageIndex);
        model.addAttribute("userList",userList);
        List<Role> roleList = null;
        roleList = roleStatus.getUserRole();
        model.addAttribute("roleList",roleList);
        model.addAttribute("queryUserName",queryUserName);
        model.addAttribute("queryUserRole",queryUserRole);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);

        return "userlist";
    }

    public String sysError(){
        return "syserror";
    }

}
